﻿using System;
using Business;
using System.Globalization;

namespace Presentation.Posts
{
    public partial class ReadPost : System.Web.UI.Page
    {
        private PostBll _postBll;
        private CommentBll _commentBll;

        protected void Page_Load(object sender, EventArgs e)
        {
            IFormatProvider formatProvider = new CultureInfo("en-gb");  
            var id = Convert.ToInt32(Request.QueryString["id"],formatProvider);
            if(!IsPostBack)
            {
                _postBll = new PostBll();
                var dataTable1 =_postBll.ReadPost(id);
                ViewAllEntry.DataSource = dataTable1;
                ViewAllEntry.DataBind();
                _commentBll = new CommentBll();
                var dataTable2 = _commentBll.ViewAllComments(id);
                CollectionPager1.PageSize = 5;
                CollectionPager1.DataSource = dataTable2.DefaultView;
                CollectionPager1.BindToControl = ViewAllComments;
                ViewAllComments.DataSource = CollectionPager1.DataSourcePaged;
            }
        }

        protected void ButtonSubmitCommentClick(object sender, EventArgs e)
        {
            IFormatProvider formatProvider = new CultureInfo("en-gb"); 
            int id = Convert.ToInt32(Request.QueryString["id"], formatProvider);
            _commentBll = new CommentBll();
            try
            {
                _commentBll.AddComment(txtUserCommentName.Text, txtComment.Text, DateTime.Now, id);
                Response.Redirect("./ReadPost.aspx?id="+id);
                //Page_Load(sender, e);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}
